﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ChairtyBank.DAL;


namespace ChairtyBank
{
    public partial class UserForm : Form
    {
        public LoginForm loginForm;
        public Int32 exisitingUser = -1;

        public UserForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
                UserEntity formUser = fillEntityFromForm();
                bool savedWithNoErros = false;
            if (formUser != null)
            {

                    if (exisitingUser != -1)
                    {
                        formUser.Id = exisitingUser;
                        formUser.UType = User.GetUser(exisitingUser).UType;
                        savedWithNoErros |= User.Update(formUser);
                    }
                    else if (User.GetUser(formUser.UName) == null)
                     {
                         savedWithNoErros |= User.Add(formUser);
                         loadAllUsers();
                     }
                    else
                    {
                        lblerror.Text = "خطـأ .. !! هذا الاسم موجود ";
                    }
                    if (savedWithNoErros)
                    {
                        MessageBox.Show("تم الحفظ بنجاح");
                        exisitingUser = User.GetUser(formUser.UName).Id;
                    }
                    else
                    {
                        lblerror.Text = " خطـأ لاتملك الصلاحية لهذه العملية" ;
                   }
                    
            }
        }

        private UserEntity fillEntityFromForm()
        {
            if (txtPassword.Text != txtConfirmPassword.Text)
            {
                lblerror.Text = "خطـأ .. !!  كلمة المرور وتأكيد كلمة المرور غير متماثلتين ";
                return null;
            }
            else if (txtPassword.Text == "")
            {
                lblerror.Text = "خطـأ .. !!  كلمة المرور وتأكيد كلمة المرور غير متماثلتين ";
                return null;
            }
            else 
            {
                UserEntity entity = new UserEntity();
                entity.UName = txtName.Text;
                entity.UPassword = txtConfirmPassword.Text;
                if (cmboType.Text == "")
                        entity.UType = User.NORMAL_USER_UType;
                    else
                        entity.UType = cmboType.Text;
               
                return entity;
            }
        }

        private void UserForm_FormClosing(object sender, FormClosingEventArgs e)
        {
        }

        private void UserForm_Load(object sender, EventArgs e)
        {
            UserEntity user = User.GetUser(exisitingUser);
            if (user.UType != User.MASTER_USER_UType)
            {
                btnClear.Enabled = false;
                btnDelete.Enabled = false;
                cmboType.Enabled = false;
            }
            else
            {
                btnClear.Enabled = true;
                btnDelete.Enabled = true;
                cmboType.Enabled = true;
                loadAllUsers();
            }
        }

        private void fillUserFormFromEntity(UserEntity user)
        {
            txtName.Text = user.UName;
            txtPassword.Text = user.UPassword;
            txtConfirmPassword.Text = user.UPassword;
            cmboType.Text = user.UType == User.ADMIN_USER_UType ? "مدير" : "مستخدم عادي";
            exisitingUser = user.Id;

        }

        public void loadAllUsers()
        {
            List<UserEntity> users = User.GetAllUser();
            gridUsers.DataSource = users;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            UserEntity formUser = fillEntityFromForm();

            if (formUser != null)
            {
                if (User.GetUser(formUser.UName) != null)
                {
                    if (User.Update(formUser))
                    {
                        //Goooooooooooooooo To 
                        MainWindow form = new MainWindow();
                        form.user = User.GetUser(formUser.UName);
                        form.Show();
                        form.loginForm = loginForm;
                        this.Close();
                        this.Dispose();
                    }
                    else
                    {
                        lblerror.Text = "خطـأ .. !! هذا المستخدم غير موجود ";
                    }

                }
                else
                {
                    lblerror.Text = "خطـأ .. !! هذا المستخدم غير موجود ";
                }
            }
        }

        private void UserForm_FormClosed(object sender, FormClosedEventArgs e)
        {
        }

        private void gridUsers_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex != -1 && e.RowIndex != -1)
            {
                UserEntity user = User.GetUser(Int32.Parse(gridUsers.Rows[e.RowIndex].Cells["clmnId"].Value.ToString()));
                if (user != null)
                {
                    fillUserFormFromEntity(user);
                }
                else
                {
                    lblerror.Text = "لايمكن نشر هذا المستخدم";
                }
            }
        }

        private void clearAllFields()
        {
            txtConfirmPassword.Text = "";
            txtName.Text = "";
            txtPassword.Text = "";
            cmboType.Text = "";
            exisitingUser = -1;
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            clearAllFields();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (exisitingUser != -1)
            {
                DialogResult result = MessageBox.Show("هل تريد حذف هذا المستخدم؟", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (DialogResult.Yes == result)
                {
                    if (User.Delete(exisitingUser))
                    {
                        MessageBox.Show("تم حذف المستخدم");
                        loadAllUsers();
                        exisitingUser = -1;
                        
                    }
                }
            }
        }

        private void button2_Click_2(object sender, EventArgs e)
        {
            loginForm.Visible = true;
            this.Close();
            this.Dispose();

        }
    }
}
